/* ###################################################################
**     Filename    : main.c
**     Processor   : S32K1xx
**     Abstract    :
**         Main module.
**         This module contains user's application code.
**     Settings    :
**     Contents    :
**         No public methods
**
** ###################################################################*/
/*!
** @file main.c
** @version 01.00
** @brief
**         Main module.
**         This module contains user's application code.
*/         
/*!
**  @addtogroup main_module main module documentation
**  @{
*/         
/* MODULE main */


/* Including necessary module. Cpu.h contains other modules needed for compiling.*/
#include "Cpu.h"

  volatile int exit_code = 0;

/* User includes (#include below this line is not maintained by Processor Expert) */

/*******************************************************************************
 includes
 *******************************************************************************/
#include "TLD7002_OTP_Program.h"

/* This example is setup to work by default with EVB. To use it with other boards
 * please comment the following line
 */
#define LED_PORT 	PORTD
#define LED_GPIO	PTD
#define SW_PORT     PORTC
#define SW_GPIO     PTC
#define BLUE_LED    0U
#define RED_LED		15U
#define GREEN_LED	16U
#define SW2         12U
#define SW3         13U

volatile uint8_t Flag_SW = 0;

void BTNPORT_IRQHandler(void)
{
	/* If SW2 is pressed */
	if (PINS_DRV_GetPortIntFlag(SW_PORT) & (1 << SW2))
	{
		PINS_DRV_ClearPinIntFlagCmd(SW_PORT, SW2);
		/* Toggle Green Led */
		PINS_DRV_TogglePins(LED_GPIO, (1 << GREEN_LED));
		Flag_SW = 1;
	}

	/* If SW3 is pressed */
	if (PINS_DRV_GetPortIntFlag(SW_PORT) & (1 << SW3))
	{
		PINS_DRV_ClearPinIntFlagCmd(SW_PORT, SW3);
		/* Toggle Red Led */
		PINS_DRV_TogglePins(LED_GPIO, (1 << RED_LED));
		Flag_SW = 2;
	}
}

/* if use S32K144EVB for controller:
 * -----mcu is S32K144_100pin
 * -----opensda-uart(lpuart_opensda) use PTC6/LPUART1_RX and PTC7/LPUART1_TX
 * -----uart-over-can(lpuart_tld7002) use PTB0/LPUART0_RX and PTB1/LPUART0_TX
 * -----can use PTE4/CAN0_RX and PTE5/CAN0_TX
 * ----- for use LPUART+CAN SBC,need PTB0 connect to PTE4,PTB1 connect to PTE5
 */


/*! 
  \brief The main function for the project.
  \details The startup initialization sequence is the following:
 * - startup asm routine
 * - main()
*/
int main(void)
{
  /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
  #ifdef PEX_RTOS_INIT
    PEX_RTOS_INIT();                   /* Initialization of the selected RTOS. Macro is defined by the RTOS component. */
  #endif
  /*** End of Processor Expert internal initialization.                    ***/

  /* Write your code here */
  /* For example: for(;;) { } */

  /* Initialize and configure clocks
   * 	-	see clock manager component for details
   */
    CLOCK_SYS_Init(g_clockManConfigsArr, CLOCK_MANAGER_CONFIG_CNT,
  						g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
    CLOCK_SYS_UpdateConfiguration(0U, CLOCK_MANAGER_POLICY_AGREEMENT);

  /* Initialize pins
   *	-	See PinSettings component for more info
   */
    PINS_DRV_Init(NUM_OF_CONFIGURED_PINS, g_pin_mux_InitConfigArr);

	/* Enable button interrupt */
    INT_SYS_InstallHandler(PORTC_IRQn, BTNPORT_IRQHandler, (isr_t *)NULL);
    INT_SYS_EnableIRQ(PORTC_IRQn);

   /* Initialize lpuart
    *	-	See lpuart component for more info
    */
#ifndef	USE_PRINTF
    LPUART_DRV_Init(INST_LPUART_OPENSDA, &lpuart_opensda_State, &lpuart_opensda_InitConfig0);
#endif
    LPUART_DRV_Init(INST_LPUART_TLD7002, &lpuart_tld7002_State, &lpuart_tld7002_InitConfig0);
    LPUART_DRV_InstallRxCallback(INST_LPUART_TLD7002, Lpuart_tld7002_RxCallback, NULL);

    /* initialize a TLD7002 network */
    TLD7002_InitNetworkInstance(&TLD7002Network);

while(1)
{
	/* use OTP write or OTP emulate*/
	if(Flag_SW == 1)
	{
#ifdef USE_EMULATE
		INT_SYS_DisableIRQ(PORTC_IRQn);
#ifdef USE_PRINTF
	    printf( "OTP EMULATE START\r\n");
#endif
	    if(OTP_Emulate(DEVICE_ADDRESS, INTERFR_DELAY_50) == true)
	    {
#ifdef USE_PRINTF
	    	printf( "OTP EMULATE SUCCESS\r\n");
#endif
	    }
	    else
	    {
#ifdef USE_PRINTF
	    	printf( "OTP EMULATE ERROR\r\n");
#endif
	    }
	    Flag_SW = 0;
		/* Toggle green Led */
		PINS_DRV_TogglePins(LED_GPIO, (1 << GREEN_LED));
		INT_SYS_EnableIRQ(PORTC_IRQn);
#else
		INT_SYS_DisableIRQ(PORTC_IRQn);
#ifdef USE_PRINTF
	    printf( "OTP WRITE START\r\n");
#endif
	    if(OTP_Write(DEVICE_ADDRESS, INTERFR_DELAY_50) == true)
	    {
#ifdef USE_PRINTF
	    	printf( "OTP WRITE SUCCESS\r\n");
#endif
	    }
	    else
	    {
#ifdef USE_PRINTF
	    	printf( "OTP WRITE ERROR\r\n");
#endif
	    }
	    Flag_SW = 0;
		/* Toggle green Led */
		PINS_DRV_TogglePins(LED_GPIO, (1 << GREEN_LED));
		INT_SYS_EnableIRQ(PORTC_IRQn);
#endif
	}
	else if(Flag_SW == 2)
	{
		INT_SYS_DisableIRQ(PORTC_IRQn);
#ifdef USE_PRINTF
	    printf( "OTP READ START\r\n");
#endif
	    if(OTP_Read(DEVICE_ADDRESS, INTERFR_DELAY_50) == true)
	    {
#ifdef USE_PRINTF
	    	printf( "OTP READ SUCCESS\r\n");
#endif
	    }
	    else
	    {
#ifdef USE_PRINTF
	    	printf( "OTP READ ERROR\r\n");
#endif
	    }
		Flag_SW = 0;
		/* Toggle Red Led */
		PINS_DRV_TogglePins(LED_GPIO, (1 << RED_LED));
		INT_SYS_EnableIRQ(PORTC_IRQn);
	}
	else if(Flag_SW > 2)
	{
		Flag_SW = 0;
	}
}


  /*** Don't write any code pass this line, or it will be deleted during code generation. ***/
  /*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/
  #ifdef PEX_RTOS_START
    PEX_RTOS_START();                  /* Startup of the selected RTOS. Macro is defined by the RTOS component. */
  #endif
  /*** End of RTOS startup code.  ***/
  /*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/
  for(;;) {
    if(exit_code != 0) {
      break;
    }
  }
  return exit_code;
  /*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/
} /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/

/* END main */
/*!
** @}
*/
/*
** ###################################################################
**
**     This file was created by Processor Expert 10.1 [05.21]
**     for the NXP S32K series of microcontrollers.
**
** ###################################################################
*/
